package gui.panel_new.evaluation.professor;

import enums.*;
import gui.panel_new.evaluation.exeptions.ExerciseNotBuiltException;
import gui.panel_new.evaluation.exeptions.PunctuationZeroException;
import j_util.Utiles;
import misc.BundlesProvider;
import model.Polisilogismo;
import model.Sylogism;
import model.eval.PolisylogismData;
import model.eval.SylogismData;
import model.eval.TestExcercise;
import model.eval.TestExercisePolisilogismo;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.ResourceBundle;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Extiende de CreateExercisePanel, permite crear ejercicios en los que se especifican
 * parametros para la construccion de un polisilogismo;
 *
 * @author Jose Martin Rozanec;
 */
public class CreatePolisilogismoPanel extends CreateExercisePanel {
    private javax.swing.JCheckBox enableExtensionsPanelCheckBox;
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JCheckBox enableFiguresPanelCheckBox;
    private javax.swing.JLabel conclusionLabel;
    private javax.swing.JPanel cuadroPanel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel premisaMayorLabel;
    private javax.swing.JLabel premisaMenorLabel;
    private javax.swing.JCheckBox enableTermsPanelCheckBox;
    private javax.swing.JLabel terminoMayorLabel;
    private javax.swing.JLabel terminoMedioLabel;
    private javax.swing.JLabel terminoMenorLabel;
    private JTextField terminoMayorField;
    private JTextField terminoMedioField;
    private JTextField terminoMenorField;
    private JButton descendenteButton;
    private JButton ascendenteButton;
    private JButton resetButton;
    private JPanel topButtonsPanel;

    private javax.swing.JTextArea textArea;

    private JCheckBox primeraFiguraCheckBox;
    private JPanel primeraFiguraPanel;
    private JCheckBox segundaFiguraCheckBox;
    private JPanel segundaFiguraPanel;
    private JCheckBox terceraFiguraCheckBox;
    private JPanel terceraFiguraPanel;
    private JCheckBox cuartaFiguraCheckBox;
    private JPanel cuartaFiguraPanel;

    private JRadioButton a1;
    private JRadioButton a2;
    private JRadioButton a3;
    private JRadioButton e1;
    private JRadioButton e2;
    private JRadioButton e3;
    private JRadioButton i1;
    private JRadioButton i2;
    private JRadioButton i3;
    private JRadioButton o1;
    private JRadioButton o2;
    private JRadioButton o3;

    private JLabel addsPoints;
    private JLabel substractPoints;
    private JTextField addsPointsField;
    private JTextField substractPointsField;

    private JButton previousButton;
    private JButton nextButton;
    private JLabel currentOverTotalLabel;

    private Color bcgColor;

    private int premisaLabelsStringLength;
    private ResourceBundle textBundle;

    private SylogismData sylogismData;
    private PolisylogismData polisylogismData;


    /**
     * @param saveButton  - saveButton;
     * @param openButton  - openButton;
     * @param fileChooser - fileChooser;
     */
    public CreatePolisilogismoPanel(JButton saveButton, JButton openButton, JFileChooser fileChooser) {
        this.saveButton = saveButton;
        this.openButton = openButton;
        this.fileChooser = fileChooser;
        fileFilter = FileFilterEnum.XML;
        testPanelEnum = TestPanelEnum.CREAR_POLISILOGISMO;
        premisaLabelsStringLength = 10;
        textBundle = BundlesProvider.getBundle(URLEnum.TEXT_BUNDLE);
        initComponents();
        reset();
    }

    /**
     * Inicializa el objeto SylogismData con valores por defecto;
     */
    private void initSylogismData(ExtensionEnum mayor, ExtensionEnum menor, ExtensionEnum conclusion) {
        this.sylogismData = new SylogismData();
        sylogismData.setFigura(FigureEnum.FIRST);
        sylogismData.setPremisaMayorExtension(mayor);
        sylogismData.setPremisaMenorExtension(menor);
        sylogismData.setConclusionExtension(conclusion);
        sylogismData.datosExigidos(enableFiguresPanelCheckBox.isSelected(), enableExtensionsPanelCheckBox.isSelected(),
                enableTermsPanelCheckBox.isSelected());
    }

    /**
     * Inicializa componentes;
     */
    private void initComponents() {
        enableFiguresPanelCheckBox = new javax.swing.JCheckBox();
        enableExtensionsPanelCheckBox = new javax.swing.JCheckBox();
        enableTermsPanelCheckBox = new javax.swing.JCheckBox();

        initSylogismData(ExtensionEnum.A, ExtensionEnum.A, ExtensionEnum.A);

        polisylogismData = new PolisylogismData();
        bcgColor = Color.WHITE;
        mainPanel = new javax.swing.JPanel();
        cuadroPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        premisaMayorLabel = new javax.swing.JLabel();
        premisaMenorLabel = new javax.swing.JLabel();
        conclusionLabel = new javax.swing.JLabel();

        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        terminoMayorLabel = new javax.swing.JLabel();
        terminoMedioLabel = new javax.swing.JLabel();
        terminoMenorLabel = new javax.swing.JLabel();
        bottomPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        ascendenteButton = new JButton(textBundle.getString(TextBundleEnum.ASCENDENTE.toString()));
        ascendenteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ascendente();
            }
        });
        ascendenteButton.setMinimumSize(new Dimension(90, 25));
        ascendenteButton.setPreferredSize(new Dimension(90, 25));

        descendenteButton = new JButton(textBundle.getString(TextBundleEnum.DESCENDENTE.toString()));
        descendenteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                descendente();
            }
        });
        descendenteButton.setMinimumSize(new Dimension(90, 25));
        descendenteButton.setPreferredSize(new Dimension(90, 25));

        resetButton = new JButton(textBundle.getString(TextBundleEnum.RESET.toString()));
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
                resetTopButtonsColor();
                resetButton.setBackground(Color.GREEN);
                enableConclusionExtensions(true);
                enablePremisaMayorExtensions(true);
            }
        });
        resetButton.setMinimumSize(new Dimension(90, 25));
        resetButton.setPreferredSize(new Dimension(90, 25));

        addsPoints = new JLabel(textBundle.getString(TextBundleEnum.SUMA.toString()));
        substractPoints = new JLabel(textBundle.getString(TextBundleEnum.RESTA.toString()));
        addsPointsField = new JTextField(3);
        addsPointsField.setEditable(false);
        substractPointsField = new JTextField(3);
        substractPointsField.setEditable(false);

        previousButton = new JButton(Utiles.createImageIcon(URLEnum.getReference(URLEnum.ANTERIOR)));
        previousButton.setMinimumSize(new Dimension(80, 25));
        previousButton.setPreferredSize(new Dimension(80, 25));
        previousButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetTopButtonsColor();
                setSylogismData(polisylogismData.previous());
                currentOverTotalLabel.setText(
                        "" + polisylogismData.getCurrentNumber() + "/" + polisylogismData.getTotalSylogisms());
                setAscendenteButtonsLabel();
            }
        });
        nextButton = new JButton(Utiles.createImageIcon(URLEnum.getReference(URLEnum.SIGUIENTE)));
        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetTopButtonsColor();
                setSylogismData(polisylogismData.next());
                currentOverTotalLabel.setText(
                        "" + polisylogismData.getCurrentNumber() + "/" + polisylogismData.getTotalSylogisms());
                setAscendenteButtonsLabel();
            }
        });
        nextButton.setMinimumSize(new Dimension(80, 25));
        nextButton.setPreferredSize(new Dimension(80, 25));
        currentOverTotalLabel = new JLabel("::/::");

        mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.SILOGISMO.toString())));

        cuadroPanel.setBackground(new java.awt.Color(255, 255, 255));
        cuadroPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.FIGURA.toString())));

        premisaMayorLabel.setText(textBundle.getString(TextBundleEnum.PREMISA_MAYOR.toString()));

        premisaMenorLabel.setText(textBundle.getString(TextBundleEnum.PREMISA_MENOR.toString()));

        conclusionLabel.setText(textBundle.getString(TextBundleEnum.CONCLUSION.toString()));

        enableFiguresPanelCheckBox.setText(textBundle.getString(TextBundleEnum.FIGURA.toString()));
        enableFiguresPanelCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetTopButtonsColor();
                enableFigures(enableFiguresPanelCheckBox.isSelected());
                sylogismData.pideFigura(enableFiguresPanelCheckBox.isSelected());
            }
        });
        enableFiguresPanelCheckBox.setMnemonic(KeyEvent.VK_C);
        enableFiguresPanelCheckBox.setSelected(true);

        enableExtensionsPanelCheckBox.setText(textBundle.getString(TextBundleEnum.EXTENSION.toString()));
        enableExtensionsPanelCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetTopButtonsColor();
                enableExtension(enableExtensionsPanelCheckBox.isSelected());
                sylogismData.pideExtension(enableExtensionsPanelCheckBox.isSelected());
            }
        });
        enableExtensionsPanelCheckBox.setMnemonic(KeyEvent.VK_A);
        enableExtensionsPanelCheckBox.setSelected(true);

        enableTermsPanelCheckBox.setText(textBundle.getString(TextBundleEnum.TERMINOS.toString()));
        enableTermsPanelCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetTopButtonsColor();
                enableTerminos(enableTermsPanelCheckBox.isSelected());
                sylogismData.pideTerminos(enableTermsPanelCheckBox.isSelected());
                if (enableTermsPanelCheckBox.isSelected()) {
                    if (ascendenteButton.getText().equals(textBundle.getString(TextBundleEnum.ADD.toString()))) {
                        manageAscendenteTerms();
                    }
                    if (descendenteButton.getText().equals(textBundle.getString(TextBundleEnum.ADD.toString()))) {
                        manageDescendenteTerms();
                    }
                }
            }
        });
        enableTermsPanelCheckBox.setMnemonic(KeyEvent.VK_R);
        enableTermsPanelCheckBox.setSelected(true);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true), textBundle.getString(TextBundleEnum.EXTENSION.toString())));
        a1 = new JRadioButton("A");
        addRadioButtonActionListener(a1, ExtensionEnum.A, PremisaTypeEnum.FIRST);
        a1.setSelected(true);
        a2 = new JRadioButton("A");
        addRadioButtonActionListener(a2, ExtensionEnum.A, PremisaTypeEnum.SECOND);
        a2.setSelected(true);
        a3 = new JRadioButton("A");
        addRadioButtonActionListener(a3, ExtensionEnum.A, PremisaTypeEnum.CONCLUSION);
        a3.setSelected(true);
        e1 = new JRadioButton("E");
        addRadioButtonActionListener(e1, ExtensionEnum.E, PremisaTypeEnum.FIRST);
        e2 = new JRadioButton("E");
        addRadioButtonActionListener(e2, ExtensionEnum.E, PremisaTypeEnum.SECOND);
        e3 = new JRadioButton("E");
        addRadioButtonActionListener(e3, ExtensionEnum.E, PremisaTypeEnum.CONCLUSION);
        i1 = new JRadioButton("I");
        addRadioButtonActionListener(i1, ExtensionEnum.I, PremisaTypeEnum.FIRST);
        i2 = new JRadioButton("I");
        addRadioButtonActionListener(i2, ExtensionEnum.I, PremisaTypeEnum.SECOND);
        i3 = new JRadioButton("I");
        addRadioButtonActionListener(i3, ExtensionEnum.I, PremisaTypeEnum.CONCLUSION);
        o1 = new JRadioButton("O");
        addRadioButtonActionListener(o1, ExtensionEnum.O, PremisaTypeEnum.FIRST);
        o2 = new JRadioButton("O");
        addRadioButtonActionListener(o2, ExtensionEnum.O, PremisaTypeEnum.SECOND);
        o3 = new JRadioButton("O");
        addRadioButtonActionListener(o3, ExtensionEnum.O, PremisaTypeEnum.CONCLUSION);

        primeraFiguraCheckBox = new JCheckBox(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_1)));
        primeraFiguraPanel = new JPanel(new GridLayout(1, 1));
        buildFigurePanel(primeraFiguraCheckBox, primeraFiguraPanel, FigureEnum.FIRST);

        segundaFiguraCheckBox = new JCheckBox(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_2)));
        segundaFiguraPanel = new JPanel(new GridLayout(1, 1));
        buildFigurePanel(segundaFiguraCheckBox, segundaFiguraPanel, FigureEnum.SECOND);

        terceraFiguraCheckBox = new JCheckBox(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_3)));
        terceraFiguraPanel = new JPanel(new GridLayout(1, 1));
        buildFigurePanel(terceraFiguraCheckBox, terceraFiguraPanel, FigureEnum.THIRD);

        cuartaFiguraCheckBox = new JCheckBox(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_4)));
        cuartaFiguraPanel = new JPanel(new GridLayout(1, 1));
        buildFigurePanel(cuartaFiguraCheckBox, cuartaFiguraPanel, FigureEnum.FOURTH);
        ButtonGroup group = new ButtonGroup();
        group.add(primeraFiguraCheckBox);
        group.add(segundaFiguraCheckBox);
        group.add(terceraFiguraCheckBox);
        group.add(cuartaFiguraCheckBox);
        primeraFiguraCheckBox.setSelected(true);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true), textBundle.getString(TextBundleEnum.TERMINOS.toString())));

        terminoMayorLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MAYOR.toString()) + ":");
        terminoMedioLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MEDIO.toString()) + ":");
        terminoMenorLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MENOR.toString()) + ":");

        terminoMayorField = new JTextField(10);
        terminoMedioField = new JTextField(10);
        terminoMenorField = new JTextField(10);

//        bottomPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true), "Sugerencias y Analisis"), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)));
        bottomPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),//new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true)
                textBundle.getString(TextBundleEnum.SUGERENCIAS.toString())));

        jScrollPane1.setViewportView(textArea);

        bottomPanel.getAccessibleContext().setAccessibleName(textBundle.getString(TextBundleEnum.ANALISIS.toString()));

        sizeTextAreas();
        layoutComponents();
        textArea.setText((new Sylogism()).getPremisaSuggestion());
    }

    /**
     * Realiza el layout de los componentes visuales;
     */
    private void layoutComponents() {
        setBackground(bcgColor);
        setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        int x = 0;
        int y = 0;
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);

        //todo start
        topButtonsPanel = new JPanel();
        topButtonsPanel.setBackground(bcgColor);
        topButtonsPanel.add(ascendenteButton);
        topButtonsPanel.add(descendenteButton);
        topButtonsPanel.add(resetButton);
        topButtonsPanel.add(previousButton);
        topButtonsPanel.add(currentOverTotalLabel);
        topButtonsPanel.add(nextButton);
        topButtonsPanel.add(addsPoints);
        topButtonsPanel.add(addsPointsField);
        topButtonsPanel.add(substractPoints);
        topButtonsPanel.add(substractPointsField);
        topButtonsPanel.setMinimumSize(new Dimension(655, 40));
        topButtonsPanel.setPreferredSize(new Dimension(655, 40));
        //todo end

        jPanel1.setLayout(new GridLayout(1, 4));

        /*      FIGURAS     */
        jPanel1.add(primeraFiguraPanel);//todo
        jPanel1.add(segundaFiguraPanel);
        jPanel1.add(terceraFiguraPanel);
        jPanel1.add(cuartaFiguraPanel);

        jPanel1.setMinimumSize(new Dimension(655, 250));
        jPanel1.setPreferredSize(new Dimension(655, 250));
        x = 0;
        y = 0;
        /*      END OF PREMISAS     */

        /*      BUTTONS     */
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = y;
        gridBagConstraints.gridwidth = 3;
        add(topButtonsPanel, gridBagConstraints);//todo delete
        gridBagConstraints.gridy = ++y;//todo delete
        add(jPanel1, gridBagConstraints);

        JPanel buttonsPanel = new JPanel(new GridLayout(1, 7));
        buttonsPanel.setBackground(bcgColor);
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(enableFiguresPanelCheckBox);
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(enableExtensionsPanelCheckBox);
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(enableTermsPanelCheckBox);
        buttonsPanel.add(new JLabel());

        gridBagConstraints.gridy = ++y;
        add(buttonsPanel, gridBagConstraints);
        /*      END OF BUTTONS     */

        jPanel2.setMinimumSize(new Dimension(260, 175));
        jPanel2.setPreferredSize(new Dimension(260, 175));
        jPanel2.setLayout(new GridLayout(3, 1));
        jPanel2.add(createExtensionRow(premisaMayorLabel, a1, e1, i1, o1));
        jPanel2.add(createExtensionRow(premisaMenorLabel, a2, e2, i2, o2));
        jPanel2.add(createExtensionRow(conclusionLabel, a3, e3, i3, o3));

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = ++y;
        add(jPanel2, gridBagConstraints);

        jPanel3 = createTerminosPanel();
        jPanel3.setMinimumSize(new Dimension(250, 175));
        jPanel3.setPreferredSize(new Dimension(250, 175));//430

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = y;
        add(jPanel3, gridBagConstraints);

        gridBagConstraints.gridx = 2;
//        add(createScoringPanel(), gridBagConstraints);

        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = ++y;

        bottomPanel.setBackground(bcgColor);
        bottomPanel.setMinimumSize(new Dimension(655, 150));
        bottomPanel.setPreferredSize(new Dimension(655, 150));
        bottomPanel.add(textArea);//jScrollPane1
        add(bottomPanel, gridBagConstraints);
    }

    private JPanel createScoringPanel() {
        JPanel scoringPanel = new JPanel();
//        scoringPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK),
//                textBundle.getString(TextBundleEnum.PUNTAJE.toString())));
        scoringPanel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        scoringPanel.add(addsPoints, gridBagConstraints);
        scoringPanel.add(addsPointsField, gridBagConstraints);
        scoringPanel.add(substractPoints, gridBagConstraints);
        scoringPanel.add(substractPointsField, gridBagConstraints);
        scoringPanel.setBackground(bcgColor);
        scoringPanel.setPreferredSize(new Dimension(200, 150));//todo
        return scoringPanel;
    }

    /**
     * Determina el tamano de los textAreas;
     */
    private void sizeTextAreas() {
        textArea.setColumns(58);
        textArea.setEditable(false);
        textArea.setRows(5);//7
        textArea.setPreferredSize(new Dimension(550, 90));//110
    }

    /**
     * Resetea el silogismo y toda la parte grafica asociada;
     */
    public void reset() {
        addsPointsField.setText("10");
        addsPointsField.setEditable(true);
        substractPointsField.setText("5");
        substractPointsField.setEditable(true);

        textArea.setText((new Sylogism()).getPremisaSuggestion());

        polisylogismData = new PolisylogismData();

        enableExtensionsPanelCheckBox.setSelected(true);
        initSylogismData(ExtensionEnum.A, ExtensionEnum.A, ExtensionEnum.A);
        setExtension(ExtensionEnum.A, a1, e1, i1, o1);
        setExtension(ExtensionEnum.A, a2, e2, i2, o2);
        setExtension(ExtensionEnum.A, a3, e3, i3, o3);

        enableTermsPanelCheckBox.setSelected(true);
        enableTerminos(true);
        terminoMayorField.setText("");
        terminoMedioField.setText("");
        terminoMenorField.setText("");
        terminoMayorField.setEditable(true);
        terminoMedioField.setEditable(true);
        terminoMenorField.setEditable(true);

        enableFiguresPanelCheckBox.setSelected(true);
        setFigura(FigureEnum.FIRST);
        primeraFiguraCheckBox.setSelected(true);
        segundaFiguraCheckBox.setSelected(false);
        terceraFiguraCheckBox.setSelected(false);
        cuartaFiguraCheckBox.setSelected(false);
        enableTermsPanelCheckBox.setSelected(true);

        currentOverTotalLabel.setText("::/::");
    }

    private JPanel createTerminosPanel() {
        JPanel panel = new JPanel();
        panel.setBackground(new java.awt.Color(255, 255, 255));
        panel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true), textBundle.getString(TextBundleEnum.TERMINOS.toString())));
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(6, 5, 5, 5);
        panel.add(terminoMayorLabel, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        panel.add(terminoMayorField, gridBagConstraints);
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridx = 0;
        panel.add(terminoMedioLabel, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        panel.add(terminoMedioField, gridBagConstraints);
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridx = 0;
        panel.add(terminoMenorLabel, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        panel.add(terminoMenorField, gridBagConstraints);
        return panel;
    }

    private void buildFigurePanel(final JCheckBox checkBox, final JPanel panel, final FigureEnum figure) {
        panel.setOpaque(true);
        panel.setBackground(bcgColor);
        checkBox.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                if (checkBox.isSelected()) {
                    panel.setBackground(Color.GREEN);
                    sylogismData.setFigura(figure);
                } else {
                    panel.setBackground(bcgColor);
                }
            }
        });
        checkBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetTopButtonsColor();
            }
        });
        panel.add(checkBox);
    }

    private JPanel createExtensionRow(JLabel label, JRadioButton a, JRadioButton e,
                                      JRadioButton i, JRadioButton o) {
        ButtonGroup group = new ButtonGroup();
        group.add(a);
        group.add(e);
        group.add(i);
        group.add(o);
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(bcgColor);
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        gridBagConstraints.gridwidth = 4;
        panel.add(label, gridBagConstraints);
        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = 4;
        panel.add(a, gridBagConstraints);
        gridBagConstraints.gridx = 5;
        panel.add(e, gridBagConstraints);
        gridBagConstraints.gridx = 6;
        panel.add(i, gridBagConstraints);
        gridBagConstraints.gridx = 7;
        panel.add(o, gridBagConstraints);
        return panel;
    }

    private void enableExtension(boolean enable) {
        a1.setEnabled(enable);
        e1.setEnabled(enable);
        i1.setEnabled(enable);
        o1.setEnabled(enable);
        a2.setEnabled(enable);
        e2.setEnabled(enable);
        i2.setEnabled(enable);
        o2.setEnabled(enable);
        a3.setEnabled(enable);
        e3.setEnabled(enable);
        i3.setEnabled(enable);
        o3.setEnabled(enable);
    }

    private void enableFigures(boolean enable) {
        primeraFiguraCheckBox.setEnabled(enable);
        segundaFiguraCheckBox.setEnabled(enable);
        terceraFiguraCheckBox.setEnabled(enable);
        cuartaFiguraCheckBox.setEnabled(enable);
    }

    private boolean checkTermsInformation() {
        if (enableTermsPanelCheckBox.isSelected()) {
            return terminoMayorField.getText().equals("") ||
                    terminoMedioField.getText().equals("") ||
                    terminoMenorField.getText().equals("");
        } else {
            return false;
        }
    }

    private void enableTerminos(boolean enable) {
        terminoMayorField.setEnabled(enable);
        terminoMedioField.setEnabled(enable);
        terminoMenorField.setEnabled(enable);
    }

    private void setSylogismData(SylogismData data) {
        setExtension(data.getPremisaMayorExtension(), a1, e1, i1, o1);
        setExtension(data.getPremisaMenorExtension(), a2, e2, i2, o2);
        setExtension(data.getConclusionExtension(), a3, e3, i3, o3);
        terminoMayorField.setText(data.getTerminoMayor());
        terminoMedioField.setText(data.getTerminoMedio());
        terminoMenorField.setText(data.getTerminoMenor());
        setFigura(data.getFigura());
    }

    private void setExtension(ExtensionEnum extension, JRadioButton a, JRadioButton e,
                              JRadioButton i, JRadioButton o) {
        a.setSelected(false);
        e.setSelected(false);
        i.setSelected(false);
        o.setSelected(false);
        switch (extension) {
            case A:
                a.setSelected(true);
                break;
            case E:
                e.setSelected(true);
                break;
            case I:
                i.setSelected(true);
                break;
            case O:
                o.setSelected(true);
                break;
        }
    }

    private void setFigura(FigureEnum figure) {
        primeraFiguraCheckBox.setSelected(false);
        segundaFiguraCheckBox.setSelected(false);
        terceraFiguraCheckBox.setSelected(false);
        cuartaFiguraCheckBox.setSelected(false);
        if (figure != null) {
            switch (figure) {
                case FIRST:
                    primeraFiguraCheckBox.setSelected(true);
                    break;
                case SECOND:
                    segundaFiguraCheckBox.setSelected(true);
                    break;
                case THIRD:
                    terceraFiguraCheckBox.setSelected(true);
                    break;
                case FOURTH:
                    cuartaFiguraCheckBox.setSelected(true);
                    break;
            }
        }
    }

    private SylogismData getSylogismData() {
        if (!enableExtensionsPanelCheckBox.isSelected()) {
            sylogismData.setPremisaMayorExtension(null);
            sylogismData.setPremisaMenorExtension(null);
            sylogismData.setConclusionExtension(null);
        }
        if (!enableFiguresPanelCheckBox.isSelected()) {
            sylogismData.setFigura(null);
        }
        if (!enableTermsPanelCheckBox.isSelected()) {
            sylogismData.setTerminoMayor(null);
            sylogismData.setTerminoMedio(null);
            sylogismData.setTerminoMenor(null);
        } else {
            sylogismData.setTerminoMayor(terminoMayorField.getText());
            sylogismData.setTerminoMedio(terminoMedioField.getText());
            sylogismData.setTerminoMenor(terminoMenorField.getText());

        }
        return sylogismData;
    }

    private void addRadioButtonActionListener(JRadioButton button, final ExtensionEnum extension, final PremisaTypeEnum premisa) {
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetTopButtonsColor();
                switch (premisa) {
                    case FIRST:
                        sylogismData.setPremisaMayorExtension(extension);
                        break;
                    case SECOND:
                        sylogismData.setPremisaMenorExtension(extension);
                        break;
                    case CONCLUSION:
                        sylogismData.setConclusionExtension(extension);
                        break;
                }
            }
        });
    }

    private void resetTopButtonsColor() {
        ascendenteButton.setBackground(bcgColor);
        descendenteButton.setBackground(bcgColor);
        resetButton.setBackground(bcgColor);
    }

    private void enableConclusionExtensions(boolean enable) {
        a3.setEnabled(enable);
        e3.setEnabled(enable);
        i3.setEnabled(enable);
        o3.setEnabled(enable);
    }

    private void enablePremisaMayorExtensions(boolean enable) {
        a1.setEnabled(enable);
        e1.setEnabled(enable);
        i1.setEnabled(enable);
        o1.setEnabled(enable);
    }

    private void setSelectedRadioButton(JRadioButton a, JRadioButton e, JRadioButton i,
                                        JRadioButton o, ExtensionEnum extension) {
        if (extension != null) {
            a.setSelected(false);
            e.setSelected(false);
            i.setSelected(false);
            o.setSelected(false);
            switch (extension) {
                case A:
                    a.setSelected(true);
                    break;
                case E:
                    e.setSelected(true);
                    break;
                case I:
                    i.setSelected(true);
                    break;
                case O:
                    o.setSelected(true);
                    break;
            }
        }
    }

    protected void setSaveActionListener() {
        class SaveSylogismDataAction extends AbstractAction {
            public SaveSylogismDataAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.SAVE_AS)));
            }

            public void actionPerformed(ActionEvent e) {
                String path = "";
                int state = 0;
                fileChooser.showSaveDialog(JOptionPane.getRootFrame());
                if (state == JFileChooser.APPROVE_OPTION) {
                    path = fileChooser.getSelectedFile().getPath();
                    Utiles.serialize(polisylogismData, Utiles.getFilePathWithExtension(path, ".psd"));
                }
            }
        }
        saveButton.setAction(new SaveSylogismDataAction());
    }

    protected void setSaveButtonEnabled() {
        saveButton.setEnabled(true);
    }

    protected void setOpenActionListener() {
        class OpenSylogismDataAction extends AbstractAction {
            public OpenSylogismDataAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.OPEN)));
            }

            public void actionPerformed(ActionEvent e) {
                String path = "";
                int state = 0;
                fileChooser.showOpenDialog(JOptionPane.getRootFrame());
                if (state == JFileChooser.APPROVE_OPTION) {
                    path = "";
                    fileChooser.getSelectedFile().getPath();
                    polisylogismData = (PolisylogismData) Utiles.deserialize(path);
                    setSylogismData(polisylogismData.getCurrent());
                }
            }
        }
        openButton.setAction(new OpenSylogismDataAction());
    }

    protected void setOpenButtonEnabled() {
        openButton.setEnabled(true);
    }

    protected void setFocusOnComponent() {
        //remains unimplemented...
    }

    public TestExcercise getExercise() throws ExerciseNotBuiltException, PunctuationZeroException {
        if (addsPointsField.getText().equals("") || substractPointsField.getText().equals("") ||
                checkTermsInformation()) {
            throw new ExerciseNotBuiltException();
        }
        if (Integer.parseInt(addsPointsField.getText()) == 0) {
            throw new PunctuationZeroException();
        }
        TestExcercise testExercise = new TestExercisePolisilogismo();

        testExercise.setExerciseData(polisylogismData);
        java.util.List<Sylogism> studentsSolution = new ArrayList<Sylogism>();
        testExercise.setStudentsSolution(studentsSolution);
        testExercise.setCorrectScore(Integer.parseInt(addsPointsField.getText()));
        if (substractPointsField.getText().equals("")) {
            substractPointsField.setText("0");
        }
        testExercise.setIncorrectScore(Integer.parseInt(substractPointsField.getText()));
        return testExercise;
    }

    public void setExercise(TestExcercise testExercise) {
        setSylogismData((SylogismData) testExercise.getExcerciseData());
        addsPointsField.setText("" + testExercise.getCorrectScore());
        substractPointsField.setText("" + testExercise.getIncorrectScore());
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("SilogismoPanel");
        frame.setContentPane(new CreateSylogismViolationPanel(new JButton(), new JButton(), new JFileChooser()));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private void ascendente() {
//        if (enableTermsPanelCheckBox.isSelected() && (terminoMayorField.getText().equals("") || terminoMedioField.getText().equals("") ||
//                terminoMenorField.getText().equals(""))) {
//            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
//                    textBundle.getString(TextBundleEnum.WARNING_COMPLETAR_DATOS.toString()));
//        } else {
        if (ascendenteButton.getText().equals(textBundle.getString(TextBundleEnum.ASCENDENTE.toString()))) {
            ascendenteButton.setText(textBundle.getString(TextBundleEnum.ADD.toString()));
            descendenteButton.setText(textBundle.getString(TextBundleEnum.DESCENDENTE.toString()));

            resetTopButtonsColor();

            if (polisylogismData.getTotalSylogisms() > 0) {
                setSelectedRadioButton(a1, e1, i1, o1, ExtensionEnum.A);
                setSelectedRadioButton(a2, e2, i2, o2, ExtensionEnum.A);
                initSylogismData(ExtensionEnum.A, ExtensionEnum.A, polisylogismData.getAscendenteExtension());
                textArea.setText(Polisilogismo.ascendenteSuggestion(polisylogismData.getAscendenteExtension()));
                enableConclusionExtensions(false);
                enablePremisaMayorExtensions(true);
                setSelectedRadioButton(a3, e3, i3, o3, polisylogismData.getAscendenteExtension());
                manageAscendenteTerms();
            }
        } else {
            if (ascendenteButton.getText().equals(textBundle.getString(TextBundleEnum.ADD.toString()))) {
                if (enableTermsPanelCheckBox.isSelected() && (terminoMayorField.getText().equals("") || terminoMedioField.getText().equals("") ||
                        terminoMenorField.getText().equals(""))) {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                            textBundle.getString(TextBundleEnum.WARNING_COMPLETAR_DATOS.toString()));
                } else {
                    ascendenteButton.setText(textBundle.getString(TextBundleEnum.ASCENDENTE.toString()));
                    polisylogismData.addAscendente(getSylogismData());
                    ascendenteButton.setBackground(Color.GREEN);
                }
            }
//            }
        }
        System.out.println("Silogismos: " + polisylogismData.getTotalSylogisms());
    }

    private void manageAscendenteTerms() {
        enableTerminos(enableTermsPanelCheckBox.isSelected());
        terminoMayorField.setEditable(true);
        terminoMenorField.setEditable(false);
//        if (!terminoMayorField.getText().equals("")) {
//            terminoMayorField.setEditable(true);
//        }
//        if (!terminoMenorField.getText().equals("")) {
//            terminoMenorField.setEditable(false);
//        }
    }

    private void descendente() {
//        if (enableTermsPanelCheckBox.isSelected() && (terminoMayorField.getText().equals("") || terminoMedioField.getText().equals("") ||
//                terminoMenorField.getText().equals(""))) {
//            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
//                    textBundle.getString(TextBundleEnum.WARNING_COMPLETAR_DATOS.toString()));
//        } else {
        if (descendenteButton.getText().equals(textBundle.getString(TextBundleEnum.DESCENDENTE.toString()))) {
            descendenteButton.setText(textBundle.getString(TextBundleEnum.ADD.toString()));
            ascendenteButton.setText(textBundle.getString(TextBundleEnum.ASCENDENTE.toString()));

            resetTopButtonsColor();

            if (polisylogismData.getTotalSylogisms() > 0) {
                setSelectedRadioButton(a2, e2, i2, o2, ExtensionEnum.A);
                setSelectedRadioButton(a3, e3, i3, o3, ExtensionEnum.A);
                initSylogismData(polisylogismData.getAscendenteExtension(), ExtensionEnum.A, ExtensionEnum.A);
                textArea.setText(Polisilogismo.ascendenteSuggestion(polisylogismData.getDescendenteExtension()));
                enableConclusionExtensions(true);
                enablePremisaMayorExtensions(false);
                setSelectedRadioButton(a1, e1, i1, o1, polisylogismData.getDescendenteExtension());
                manageDescendenteTerms();
            }
        } else {
            if (descendenteButton.getText().equals(textBundle.getString(TextBundleEnum.ADD.toString()))) {
                if (enableTermsPanelCheckBox.isSelected() && (terminoMayorField.getText().equals("") || terminoMedioField.getText().equals("") ||
                        terminoMenorField.getText().equals(""))) {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                            textBundle.getString(TextBundleEnum.WARNING_COMPLETAR_DATOS.toString()));
                } else {
                    descendenteButton.setText(textBundle.getString(TextBundleEnum.DESCENDENTE.toString()));
                    polisylogismData.addDescendente(getSylogismData());
                    descendenteButton.setBackground(Color.GREEN);
                }
            }
//            }
        }
        System.out.println("Silogismos: " + polisylogismData.getTotalSylogisms());
    }

    private void manageDescendenteTerms() {
        enableTerminos(enableTermsPanelCheckBox.isSelected());
        terminoMayorField.setEditable(false);
        terminoMenorField.setEditable(true);
//        if (terminoMayorField.getText().equals("")) {
//            terminoMayorField.setEditable(false);
//        }
//        if (terminoMenorField.getText().equals("")) {
//            terminoMenorField.setEditable(true);
//        }
    }

    private void setAscendenteButtonsLabel() {
        ascendenteButton.setText(textBundle.getString(TextBundleEnum.ASCENDENTE.toString()));
        descendenteButton.setText(textBundle.getString(TextBundleEnum.DESCENDENTE.toString()));
    }

}


